package com.tg.app;
import android.content.Context;
import android.graphics.Bitmap;

import com.tange.core.device.facade.DeviceFacade;
import com.tg.appcommon.sdk.TGErrorCode;
import com.tg.app.camera.Camera;
import com.tg.app.helper.DeviceHelper;
import com.tange.base.toolkit.NetworkUtil;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGApplicationBase;

public class TGDevice {
    private boolean check(String uuid, TGDeviceListener listener){
        if (!TGSdk.getInstance().isInitSdk()){
            if (listener != null){
                listener.onResult(false, TGErrorCode.ERROR_NOT_INIT_SDK, "TGSdk::initSdk fail or no call");
            }
            return false;
        }
        if (StringUtils.isEmpty(uuid) || uuid.length() != 12){
            listener.onResult(false, TGErrorCode.ERROR_UUID_INVALID,"uuid invalid");
            return false;
        }
        return true;
    }

    public void openDevice(Context context, String uuid, TGDeviceListener listener){
        if (check(uuid, listener)){
            DeviceHelper.openDevice(context, uuid, listener);
        }
    }

    public void add4gDevice(Context context, String uuid, TGDeviceListener listener){
        if (check(uuid, listener)){
            DeviceHelper.add4gDevice(context, uuid);
        }
    }

    public void addWifiDeviceByQRCode(Context context, TGDeviceListener listener){
        if (!TGSdk.getInstance().isInitSdk()){
            if (listener != null){
                listener.onResult(false, TGErrorCode.ERROR_NOT_INIT_SDK, "TGSdk::initSdk fail or no call");
            }
        }else {
            DeviceHelper.addWifiDeviceByQRCode(context);
        }

    }

    public void addWifiDeviceByHotspot(Context context, TGDeviceListener listener){
        if (!TGSdk.getInstance().isInitSdk()){
            if (listener != null){
                listener.onResult(false, TGErrorCode.ERROR_NOT_INIT_SDK, "TGSdk::initSdk fail or no call");
            }
        }else {
            DeviceHelper.addWifiDeviceByHotspot(context);
        }
    }

    public Bitmap  getQRCodeImageWifiDevice(String wifi, String pwd, int width, int height){
        return DeviceHelper.getQRCodeImageWifiDevice(wifi, pwd, width, height);
    }

    public String getWifiDeviceQRString(String wifi, String pwd){
        return DeviceHelper.getWifiDeviceQRString(wifi, pwd);
    }

    public void queryBindState(String code, final TGDevice.TGDeviceListener listener){
        String[] str = code.split("\n");
        String token = code;
        if (str.length == 4){
            token = str[0];
        }
        DeviceHelper.queryBindState(token, listener);
    }

    public void showDeviceMessage(Context context, String uuid, TGDeviceListener listener){
        if (check(uuid, listener)){
            DeviceHelper.showDeviceMessage(context, uuid, listener);
        }
    }

    public boolean isNetwork(Camera camera){
        return NetworkUtil.isNetworkConnected(TGApplicationBase.getApplication()) && camera != null && camera.isConnected();
    }

    public boolean isNetwork(DeviceFacade deviceFacade){
        return NetworkUtil.isNetworkConnected(TGApplicationBase.getApplication()) && deviceFacade != null && deviceFacade.connected();
    }

    private TGDevice() { }
    public static TGDevice getInstance() {
        return SingletonHolder.sInstance;
    }
    private static class SingletonHolder {
        private static TGDevice sInstance = new TGDevice();
    }

    public interface TGDeviceListener {
        void onResult(boolean result, int errorCode, String errorInfo);
    }
}
